package com.mef.smsplat.web.controller.sys.user;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc24.util.IDUtil;
import com.hc24.util.StringUtil;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.AdminInfoDomain;
import com.mef.smsplat.domain.OperatorInfoDomain;
import com.mef.smsplat.domain.SysAdminInfoDomain;
import com.mef.smsplat.exception.BusinessException;
import com.mef.smsplat.service.AdminInfoService;
import com.mef.smsplat.util.Constants;
import com.mef.smsplat.web.controller.base.Json;

@Controller
@RequestMapping("/sys/admin")
public class AdminInfoController {
	private AdminInfoService adminInfoService;
	
	public void setAdminInfoService(AdminInfoService adminInfoService) {
		this.adminInfoService = adminInfoService;
	}

	@ResponseBody
	@RequestMapping("/login.sys")
	public Json login(HttpSession session,String adminName,String adminPassword){
		if(adminName==null||"".equals(adminName.trim()))
			throw new BusinessException("用户名不能为空!");
		if(adminPassword==null||"".equals(adminPassword.trim()))
			throw new BusinessException("密码不能为空!");
		AdminInfoDomain admin= adminInfoService.login(adminName, adminPassword);
		if(admin!=null){
			session.setAttribute(Constants.SESSION_TYPE, Constants.SESSION_ADMIN);
			session.setAttribute(Constants.SESSION_ADMIN, admin);
			Json json=new Json();
			json.setData(admin);
			return json;
			
		}else{
			Json json=new Json();
			json.setSuccess(false);
			json.setMsg("用户名或者密码错误!");
			return json;
		}
	}
	
	@ResponseBody
	@RequestMapping("/query.sys")
	public Page<AdminInfoDomain> query(String adminName,DataGridModel dgm){
		return adminInfoService.query(adminName, dgm);
	}
	
	@ResponseBody
	@RequestMapping("/add.sys")
	public Json add(HttpSession session,AdminInfoDomain admin){
		if(admin.getAdminName()==null || "".equals(admin.getAdminName().trim())){
			throw new BusinessException("用户名不能为空");
		}
		if(admin.getTrueName()==null || "".equals(admin.getTrueName().trim())){
			throw new BusinessException("真实姓名不能为空");
		}
		if(admin.getDepart()==null || "".equals(admin.getDepart().trim())){
			throw new BusinessException("部门不能为空");
		}
		if(admin.getRoleId()==null || "".equals(admin.getRoleId().trim())){
			throw new BusinessException("角色不能为空");
		}
		if(admin.getTel()==null || "".equals(admin.getTel().trim())){
			throw new BusinessException("联系电话不能为空");
		}
		if(admin.getValidBeginTime()==null){
			throw new BusinessException("开始时间不能为空");
		}
		if(admin.getValidEndTime()==null){
			throw new BusinessException("结束时间不能为空");
		}
		if(!adminInfoService.isUnique(admin.getAdminName(), admin.getAdminId())){
			throw new BusinessException("此账号已经存在");
		}
		//获取当前登录用户  并且获得createId 和 createName
		String createId = "";
		String createName = "";
		if(Constants.SESSION_SYS_ADMIN.equals(session.getAttribute(Constants.SESSION_TYPE))){
			SysAdminInfoDomain adm = (SysAdminInfoDomain) session.getAttribute(Constants.SESSION_SYS_ADMIN);
			createId = adm.getSysAdminId();
			createName = adm.getAdminName();
		}else if(Constants.SESSION_ADMIN.equals(session.getAttribute(Constants.SESSION_TYPE))){
			AdminInfoDomain adm = (AdminInfoDomain) session.getAttribute(Constants.SESSION_ADMIN);
			createId = adm.getAdminId();
			createName = adm.getAdminName();
		}else if(Constants.SESSION_OPERATOR.equals(session.getAttribute(Constants.SESSION_TYPE))){
			OperatorInfoDomain ope = (OperatorInfoDomain) session.getAttribute(Constants.SESSION_OPERATOR);
			createId = ope.getOperatorId();
			createName = ope.getOperatorName();
		}else{
			createId = "1";
			createName = "no";
		}
		
		admin.setAdminId(IDUtil.next()+"");
		admin.setAdminPassword(StringUtil.md5(Constants.PASSWORD));//默认密码
		admin.setCreateTime(new Date());
		admin.setCreateId(createId);
		admin.setCreateName(createName);
		admin.setStatus(Constants.NORMAL);
		adminInfoService.save(admin);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/update.sys")
	public Json update(AdminInfoDomain admin){
		if(admin.getAdminName()==null || "".equals(admin.getAdminName().trim())){
			throw new BusinessException("用户名不能为空");
		}
		if(admin.getTrueName()==null || "".equals(admin.getTrueName().trim())){
			throw new BusinessException("真实姓名不能为空");
		}
		if(admin.getDepart()==null || "".equals(admin.getDepart().trim())){
			throw new BusinessException("部门不能为空");
		}
		if(admin.getRoleId()==null || "".equals(admin.getRoleId().trim())){
			throw new BusinessException("角色不能为空");
		}
		if(admin.getTel()==null || "".equals(admin.getTel().trim())){
			throw new BusinessException("联系电话不能为空");
		}
		if(admin.getValidBeginTime()==null){
			throw new BusinessException("开始时间不能为空");
		}
		if(admin.getValidEndTime()==null){
			throw new BusinessException("结束时间不能为空");
		}
		if(!adminInfoService.isUnique(admin.getAdminName(), admin.getAdminId())){
			throw new BusinessException("此账号已经存在");
		}
		adminInfoService.update(admin);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/del.sys")
	public Json del(String[] ids){
		adminInfoService.removeById(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/resetPassword.sys")
	public Json resetPassword(String[] ids){
		adminInfoService.resetPassword(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/enable.sys")
	public Json enable(String[] ids){
		adminInfoService.enable(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/disable.sys")
	public Json disable(String[] ids){
		adminInfoService.disable(ids);
		return new Json();
	}
}
